home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14979 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  51 lines

  1. Newsgroups: comp.lang.c,comp.unix.programmer
  2. Path: new-news.sprintlink.net!eskimo!scs
  3. From: scs@eskimo.com (Steve Summit)
  4. Subject: implicit zero conditionals (was: Q: '\n' character)
  5. X-Nntp-Posting-Host: eskimo.com
  6. Message-ID: <DpyM3K.5A6@eskimo.com>
  7. Followup-To: comp.lang.c
  8. Sender: news@eskimo.com (News User Id)
  9. Organization: schmorganization
  10. References: <4kpd2g$eeb@masala.cc.uh.edu> <1996Apr14.014133.22865@sq.com> <4ku9he$5ee@dawn.mmm.com>
  11. Date: Tue, 16 Apr 1996 14:40:31 GMT
  12.  
  13. [Followups redirected to comp.lang.c only, although the topic is
  14. an old one there.]
  15.  
  16. In article <4ku9he$5ee@dawn.mmm.com>, cjsonnack@mmm.com (Chris Sonnack) writes:
  17. > Mark Brader (msb@sq.com) wrote:
  18. >>>>>  if (ptr) *ptr = '\0';
  19. >>> Ok, I've got a question at this point.  Is it really proper to say
  20. >>> if (ptr)?
  21. >> Yes.  Please reread the comp.lang.c FAQ list.
  22. > I just finished a Java class...
  23. > Java has (go figure) eliminated the above ability. The if()
  24. > (and the loops) MUST HAVE a boolean value there, NOT a zero/non-zero.
  25. > In Java, you MUST write:  if (x != 0) { /** do stuff **/ }
  26.  
  27. For what it's worth, many of us prefer that style in C as well,
  28. and write if(p != 0) or if(p != NULL) as a matter of choice.
  29. The argument is that the controlling expression of an if
  30. statement is of "conceptual Boolean type", which is what !=
  31. and the other relational and logical operators return, and which
  32. a pointer is not.  (Programmers of this bent will even write
  33. if(x != 0) where x is a general-purpose int, reserving the
  34. condensed form for cases like if(is_vegetable) where the
  35. variable is_vegetable is itself of conceptual Boolean type.)
  36.  
  37. But before six people jump on me, I'll save them the trouble
  38. by saying here that they're not alone, and that plenty of C
  39. programmers have no problem with if(x), where x has an integral,
  40. floating-point, or pointer type.  The interpretation is no longer
  41. "is x true?" but "is x nonzero?" (which is, of course, precisely
  42. the interpretation C is using), or, in the case of pointers,
  43. perhaps "is x valid?" (although a non-null pointer is necessarily
  44. valid only if you're careful to initialize all pointers correctly
  45. and to explicitly set them to null after invalidating them).
  46.  
  47.                     Steve Summit
  48.                     scs@eskimo.com
  49.